<?php
class Auth extends Controller {

	var $MethodAccessControlType	= 'allow';
	var $AccessMethods				= array('loginAdmin','executeLoginAdmin','executeLogout','viewAdminInfo','executeModifyAdminInfo');
	
	var $sess_id;
	var $admin_id;
	var $admin_name;
	var $isAdmin = false;

	var $Dao = null;	

	function Auth($Dao = null) {

		session_start();

		$this->isAdmin = false;

		$this->setSessionObject();
		$this->initController();

		//----------------------- 다른 클래스에서 불러 사용할것을 대비하여 Dao object를 전달받은경우 그대로 사용할 수 있도록 설계
		$this->Dao = $Dao;
		if($this->Dao == null) {
			$this->getControllerInstance("Dao");
			$this->Dao->Connect();
		}


	}

	function setSessionObject() {
		$this->admin_id 	= $_SESSION['sess_admin_id'];
		$this->admin_name	= $_SESSION['sess_admin_name'];
		$this->isAdmin		= $_SESSION['isAdmin'];
	}

	function isAdmin() {

		if($this->sess_admin_id) {
			return true;
		} else {
			return false;
		}
		
	}

	function loginAdmin() {

		$this->refer_url					= $this->args[0];
		if(!$this->refer_url) {
			$this->refer_full_url			= $_SERVER['HTTP_REFERER'];
			$this->refer_full_url_array		= parse_url($this->refer_full_url);
			$this->refer_url				= $this->refer_full_url_array['path'];
			$this->refer_url				= base64_encode($this->refer_url);
		}

		$this->setLayout("sub");	//기본레이아웃 셋
		$this->layout['left']	= "/inc/left.html";		//기본 left 메뉴 가져오기
		$this->result_uri		= "/admin/login.html";
		$this->dispatcher();		
	}

	function executeLoginAdmin() {

		$this->admin_id		= $_POST['admin_id'];
		$this->admin_pw		= $_POST['admin_pw'];
		$this->refer_url	= $_POST['refer_url'];

		$query = "Select * From admin Where admin_id = ? and admin_pw = password(?)";
		$param = array($this->admin_id,$this->admin_pw);
		$this->Dao->executeBindingQuery($query,$param);

		if(!$this->Dao->getNumRows()) {
			$this->ErrorBackMsg("아이디,비밀번호가 일치하지 않습니다. 다시한번입력해주세요");
			exit;
		}

		$this->admin_info = $this->Dao->getFetchObject();
		
		$_SESSION['sess_admin_id']		= $this->admin_info->admin_id;
		$_SESSION['sess_admin_name']	= $this->admin_info->admin_name;
		$_SESSION['isAdmin']			= true;
		
		if($this->refer_url) {
			$this->result_uri = base64_decode($this->refer_url);
		} else {
			$this->result_uri = "/";
		}

		$this->redirect();

	}

	function getMyInfo() {


		$query = " Select * From admin Where admin_id = ? ";
		$param = $this->admin_id;
		$this->Dao->executeBindingQuery($query,$param);

		return $this->Dao->getFetchObject();

	}


	function executeLogout() {
		
		$_SESSION['sess_admin_id']		= null;
		$_SESSION['sess_admin_name']	= null;
		$_SESSION['isAdmin']			= false;

		$this->result_uri = "/";
		$this->redirect();

	}

	function checkAdmin() {

		if(!$this->isAdmin) {
			$this->ErrorBackMsg("잘못된 호출입니다.");
			exit;
		}
		
	}


	function viewAdminInfo() {

		$this->checkAdmin();

		$this->admin_info			= $this->getMyInfo();

		$this->setLayout("sub");	//기본레이아웃 셋
		$this->layout['left']		= "/inc/left.html";		//기본 left 메뉴 가져오기
		$this->result_uri			= "/admin/admin_info.html";
		$this->dispatcher();		

	}

	function executeModifyAdminInfo() {
		
		$this->checkAdmin();

		$this->admin_name			= $_POST['admin_name'];
		$this->admin_email			= $_POST['admin_email'];
		$this->admin_pw				= $_POST['admin_pw'];

		$this->mod_admin_pw			= $_POST['mod_admin_pw'];
		$this->new_admin_pw			= $_POST['new_admin_pw'];
		$this->new_admin_pw2		= $_POST['new_admin_pw2'];


		//먼저 비밀번호를 체크한다.
		$query = "Select count(*) From admin Where admin_id = ? and admin_pw = password(?)";
		$param = array($this->admin_id,$this->admin_pw);
		$this->Dao->executeBindingQuery($query,$param);

		if(!$this->Dao->getResult()) {
			$this->ErrorBackMsg("비밀번호가 일치하지 않습니다. 다시한번입력해주세요");
			exit;
		}

		//개인정보를 변경한다.
		if($this->mod_admin_pw) {
			$query = "Update admin Set admin_name = ?, admin_email = ?, admin_pw = password(?) Where admin_id = ?";
			$param = array($this->admin_name,$this->admin_email,$this->new_admin_pw,$this->admin_id);
		} else {
			$query = "Update admin Set admin_name = ?, admin_email = ? Where admin_id = ?";
			$param = array($this->admin_name,$this->admin_email,$this->admin_id);
		}
		$this->Dao->executeBindingQuery($query,$param);

		//변경후 조회화면으로 이동
		//$this->result_uri = "/Auth/viewAdminInfo";
		//$this->redirect();

		$this->alertGoMsg("수정이 완료되었습니다.","/Auth/viewAdminInfo");
		exit;

	}


	function alertGoMsg($msg,$url) {

		echo "
			<html>
			<head><title>$msg</title>
			<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
				<script>
					function go() {
						alert('".$msg."');
						document.location.href='".$url."'
					}
				</script>
			</head>
			<body onLoad='go()'>
				<!-- Error Page...-->
			</body>
			</html>
		";
		exit;
		
	}




	
}
?>